package cn.ava.ymcc.mq.consumer;

import cn.ava.ymcc.domain.MessageStation;
import cn.ava.ymcc.service.IMessageStationService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-station-consumer",//消费者组
        topic = "topic-course-online",//topic
        selectorExpression = "tags-station",//tags
        messageModel = MessageModel.CLUSTERING//集群模式，一个消息只会给到一个消费者
        //还有一个广播模式，一个消息可以被多个消费者消费
)
public class CourseOnlineStationConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMessageStationService messageStationService;


    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        MessageStation messageStation = JSONObject.parseObject(message, MessageStation.class);
        // 发送站内信
        log.info("推送站内新课程发布消息....");
        // 保存日志
        messageStationService.insert(messageStation);

    }


}
